
var e = getApp();
const {
  fabuWxUser
} = require('../../common/api/user')
const api = require('../../common/utils/request.js')
Component({
    properties: {
      show: {
        value: false,
        type: Boolean
    },
    },
    
    data: {
      page: 1,
      row: 1000,
       list: [],
       isAll: false,//是否全选
       isAllDisabled: false,//是否全部不可选
       text: '',
       last_page: 1,
    },
    ready: function() {
      this.getList();
    },
    methods: {
      
      search(){
        this.setData({
          list: []
        })
        this.getList();
      },
      loadMore() {
        console.log('到底了')
        if(this.data.page < this.data.last_page){
          this.data.page++;
          this.setData({
            page: this.data.page
          })
          this.getList()
        }else {
          wx.showToast({
            title: '已加载全部',
            icon: 'none'
          })
        }
      },
      allSelect(){
        if(this.data.isAll){
          this.data.list.forEach(item => {
            item.checked=false
          })
          this.setData({
            list: this.data.list,
            isAll: false
          })
        }else {
          this.data.list.forEach(item => {
            item.checked=item.disabled ? false :true
          })
          this.setData({
            list: this.data.list,
            isAll: true
          })
        }
        console.log(this.data.list,'全选')
      },
      changeText(e){
        this.setData({
          text: e.detail.value
        })
      },
      getList() {
        var _this = this;
        api.post(fabuWxUser, {
          page: this.data.page,
          row: this.data.row,
          pt_id: e.globalData.ptId,
          uid: wx.getStorageSync('yz_uid'),
          text: this.data.text
        }).then(res => {
          //成功时回调函数
          console.log(res, 'fabuWxUser')
          if (res.errno == 0) {
            var data = res.data.data;
            var arr = data.map(item =>{
              return {
                ...item,
                disabled: (item.is_login == 1 && parseFloat(item.login_time) > 24) ? false : true,
                // disabled: item.is_login == 0 ? false : true,
                checked: false
              }
            })
            
            this.setData({
              list: [...this.data.list,...arr],
              last_page: res.data.last_page
            })
            const allDisabledValid = this.data.list.every(item => item.disabled == true);
            if(allDisabledValid){
              this.setData({
                isAllDisabled: true
              })
            }else {
              this.setData({
                isAllDisabled: false
              })
            }
            console.log(this.data.list,'listtttttttttt')
          }
        }).catch(err => {
          //失败时回调函数
          console.log(err)
        })
  
      },
      changeType(e){
        var index = e.currentTarget.dataset.index;
        var item = e.currentTarget.dataset.item;
        if(item.disabled){
          return
        }
        this.data.list[index].checked = !item.checked
        console.log(this.data.list)
        this.setData({
          list: this.data.list
        })
        const allChecked = this.data.list.every(item => item.checked === true);
        if(allChecked){
          this.setData({
            isAll: true
          })
        }else {
          this.setData({
            isAll: false
          })
        }
      },
      popupSpecsbtn: function(t) {
        console.log(t);
        this.triggerEvent("closeShow");
        this.setData({
            show: false
        });
      },
      submit: function() {
        console.log(this.data.list,'确定')
        var arr = []
        this.data.list.forEach(item => {
          if(item.checked){
            arr.push(item.id)
          }
        })
        console.log(arr,'arrrrrr')
        if(arr.length == 0){
          wx.showToast({
            title: '请选择账号',
            icon: 'none'
          })
          return
        }
        // return
        this.triggerEvent("ok",arr);
        // this.setData({
        //     show: false
        // });
      },
    }
});